//
//  HotShopsViewController.m
//  project_delicious
//
//  Created by 章历杰 on 15/3/25.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HotShopsViewController.h"
#import "AFNetworking.h"
#import "AllOfHotShopsCell.h"
#import "OneOfShopsViewController.h"
#import "MBProgressHUD.h"
@interface HotShopsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) MBProgressHUD *HUD;
@end

@implementation HotShopsViewController
- (void)dealloc
{
    [_tableView release];
    [_array release];
    [_num release];
    [_HUD release];
    [super dealloc];
}
//解析数据
- (void)startAnalyze:(NSString *)str
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.array = [[responseObject objectForKey:@"data"] objectForKey:@"app_share_hot_shop"];
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, self.view.frame.size.height-65) style:UITableViewStylePlain];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.separatorColor = [UIColor darkGrayColor];
        self.tableView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:self.tableView];
        [self.tableView release];
        
        //返回主页
        UIImageView *backMain = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-15, 30, 30, 30)];
        backMain.image = [UIImage imageNamed:@"backToRe2.png"];
        backMain.layer.masksToBounds = YES;
        backMain.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapMain = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backMain)];
        [backMain addGestureRecognizer:tapMain];
        [tapMain release];
        [self.view addSubview:backMain];
        [backMain release];
        
        //自定义返回View
        UIView *back = [[UIView alloc] initWithFrame:CGRectMake(10, 30, 30, 30)];
        back.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backToRecomm.png"]];
        back.layer.masksToBounds = YES;
        back.layer.cornerRadius = 15;
        back.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackAction)];
        [back addGestureRecognizer:tap];
        [tap release];
        [self.view addSubview:back];
        [back release];
        
        [_HUD removeFromSuperview];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"失败");
    }];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"shopDetailBackG.jpg"]];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.array = [NSMutableArray array];
    
    NSString *string1 = @"http://yinweiapi.paidui.com:81/jms/fetchData.do?procedureName=app_share_hot_shop&parameters=%7B%22_end%22%3A%2215%22%2C%22_member_share_id%22%3A%22";
    NSString *string2 = @"%22%2C%22_start%22%3A%220%22%7D";
    
    NSString *str = [NSString stringWithFormat:@"%@%@%@", string1,self.num,string2];
    
    [self startAnalyze:str];
    
    
    self.HUD = self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    _HUD.labelText = @"Loading";
    //_HUD.color = [UIColor clearColor];
    _HUD.mode = MBProgressHUDModeIndeterminate;
    [self.view addSubview:_HUD];
    [_HUD release];
    [_HUD show:YES];
    
    //返回主页
    UIImageView *backMain = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-15, 30, 30, 30)];
    backMain.image = [UIImage imageNamed:@"backToRe2.png"];
    backMain.layer.masksToBounds = YES;
    backMain.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapMain = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backMain)];
    [backMain addGestureRecognizer:tapMain];
    [tapMain release];
    [self.view addSubview:backMain];
    [backMain release];
    
    //自定义返回View
    UIView *back = [[UIView alloc] initWithFrame:CGRectMake(10, 30, 30, 30)];
    back.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backToRecomm.png"]];
    back.layer.masksToBounds = YES;
    back.layer.cornerRadius = 15;
    back.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackAction)];
    [back addGestureRecognizer:tap];
    [tap release];
    [self.view addSubview:back];
    [back release];
    
    
}
//自定义的返回View的方法
- (void)tapBackAction
{
    [self.navigationController popViewControllerAnimated:NO];
}
//返回主页
- (void)backMain
{
    [self.navigationController popToRootViewControllerAnimated:NO];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"reuse";
    AllOfHotShopsCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[AllOfHotShopsCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
    }
    cell.mDic = [self.array objectAtIndex:indexPath.item];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    OneOfShopsViewController *one = [[OneOfShopsViewController alloc] init];
    one.num = [[self.array objectAtIndex:indexPath.row] objectForKey:@"shop_id"];
    
    one.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:one animated:NO];
    //one.hidesBottomBarWhenPushed = NO;
    [one release];
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
